package com.zhongxin.wymovieticket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.zhongxin.wymovieticket.location.City;
import com.zhongxin.wymovieticketfragment.ActivityFragment;
import com.zhongxin.wymovieticketfragment.CinemaFragment;
import com.zhongxin.wymovieticketfragment.ClientFragment;
import com.zhongxin.wymovieticketfragment.MovieFragment;
import com.zhoujing.Entity.CinemaDetailEntity;
import com.zhoujing.Entity.CountryCity;
import com.zhoujing.utils.JsonUtils;
import com.zhoujing.utils.JsonUtils.JsonObjectCallBack;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.Toast;

/**
 * 主界面
 * 
 * @author jqw
 * 
 */
public class MainActivity extends FragmentActivity implements OnClickListener {

	private final static String CITYURL = "http://piao.163.com/m/base_data.html?updateTime=&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8&city=440300";
	private RadioButton movieBt, cinemaBt, activityBt, clientBt;
	private FragmentManager fmManager;
	private FragmentTransaction ftTransaction, ftTransaction2;
	private MovieFragment movieFragment, movieFragment2;
	private CinemaFragment cinemaFragment;
	private ClientFragment clientFragment;
	private ActivityFragment activityFragment;
	private City city;
	//城市详细信息实体
		private CountryCity countryCity;
		//影院详情
		private CinemaDetailEntity cinemaDetailEntity;
		private RequestQueue queue;
		private ProgressDialog dialog;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
        queue = Volley.newRequestQueue(this);
        dialog = new ProgressDialog(this);
        dialog.setTitle("正在加载");
        dialog.setMessage("正在加载数据，请稍后");
		movieBt = (RadioButton) findViewById(R.id.main_activity_menu_movie);
		cinemaBt = (RadioButton) findViewById(R.id.main_activity_menu_cinema);
		activityBt = (RadioButton) findViewById(R.id.main_activity_menu_activities);
		clientBt = (RadioButton) findViewById(R.id.main_activity_menu_client);
		movieBt.setOnClickListener(this);
		cinemaBt.setOnClickListener(this);
		activityBt.setOnClickListener(this);
		clientBt.setOnClickListener(this);
		fmManager = getSupportFragmentManager();
		movieFragment = new MovieFragment();
		movieFragment2 = new MovieFragment();
		cinemaFragment = new CinemaFragment();
		clientFragment = new ClientFragment();
	    activityFragment = new ActivityFragment();
		ftTransaction2 = fmManager.beginTransaction();
		ftTransaction2.replace(R.id.main_activity_replace, movieFragment);
		ftTransaction2.addToBackStack("movieFragment");
		ftTransaction2.commit();
		// 数据传递
		Intent intent = getIntent();
		String cityString=intent.getStringExtra("cityname");	
		Bundle bundle = new Bundle();
		bundle.putString("cityname2",cityString);
		movieFragment.setArguments(bundle);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ftTransaction = fmManager.beginTransaction();
		switch (v.getId()) {
		case R.id.main_activity_menu_movie:
			ftTransaction.replace(R.id.main_activity_replace, movieFragment);
			//ftTransaction.addToBackStack("movieFragment2");
			break;
		case R.id.main_activity_menu_cinema:
			//得到数据
			dialog.show();
			 GetData();	

			break;
		case R.id.main_activity_menu_activities:
			ftTransaction.replace(R.id.main_activity_replace, activityFragment);
			ftTransaction.addToBackStack("clientFragment");
			break;
		case R.id.main_activity_menu_client:
			ftTransaction.replace(R.id.main_activity_replace, clientFragment);
			ftTransaction.addToBackStack("activityFragment");
			break;
		default:
			break;
		}
		ftTransaction.commit();
	}

    private void GetData() {
        // TODO Auto-generated method stub
        //得到影院的信息
        JsonUtils.parseJsonObject(this, CITYURL, new JsonObjectCallBack() {



            @Override
            public void jsonObjectCallBack(org.json.JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject != null) {
                    countryCity = new CountryCity();
                    countryCity = JSON.parseObject(jsonObject.toString(),
                            CountryCity.class);
                    if (countryCity!=null) {
                        dialog.dismiss();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("countryCity", countryCity);
                        ftTransaction = fmManager.beginTransaction();
                        if (cinemaFragment==null) {
                            cinemaFragment=new CinemaFragment();
                        }
                        cinemaFragment.setArguments(bundle);
                        ftTransaction.replace(R.id.main_activity_replace, cinemaFragment);
                        ftTransaction.commit();
                    }
                }
            }
        }, queue);

    }


}
